package com.ww.springboot.dd.controller;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import cn.hutool.http.HttpRequest;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2025-03-14 16:26
 */
@Slf4j
@RestController
@RequestMapping("/pic")
@AllArgsConstructor
public class PicController {

    @GetMapping("/download-zip")
    public ResponseEntity<ByteArrayResource> downloadZip(@RequestParam String imageUrl) {
        try {
            // 下载图片
            byte[] imageBytes = HttpRequest.get(imageUrl).execute().bodyBytes();

            // 创建压缩包
            byte[] zipBytes = ImageDownloaderAndZipper2.createZipFile(imageBytes, "image.jpg");

            // 返回压缩包
            ByteArrayResource resource = new ByteArrayResource(zipBytes);
            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=images.zip")
                    .contentType(MediaType.APPLICATION_OCTET_STREAM)
                    .contentLength(zipBytes.length)
                    .body(resource);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(null);
        }
    }


    @GetMapping("/download-ziptest")
    public void downloadZiptest(@RequestParam String imageUrl) {
        System.out.println(imageUrl);
    }
}
